package com.joker.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joker.entity.Chat;
import com.joker.mapper.ChatMapper;
import com.joker.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 聊天记录表 服务实现类
 * </p>
 *
 * @author joker
 * @since 2021-06-03
 */
@Service
public class ChatServiceImpl implements ChatService {
    @Autowired
    private ChatMapper chatMapper;

    @Override
    public List<Chat> findByUserIdAndToUserId(String userId, String toUserId) {
        return chatMapper.findAllToUserOrToUser(userId,toUserId);
    }

    @Override
    public Integer save(Chat chat) {
        LocalDateTime localDateTime = LocalDateTime.now();
        chat.setChatDate(localDateTime);
        return chatMapper.insert(chat);
    }

    @Override
    public void setReadFlag(String userId, String toUserId) {
        chatMapper.setReadFlagByUserIdAndToUserId(userId,toUserId);
    }

    @Override
    public Integer delete(String userId, String toUserId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("chat_send",userId);
        queryWrapper.eq("chat_receive",toUserId);
        return chatMapper.delete(queryWrapper);
    }
}
